package com.android.infantschool.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.InfromBean;
import com.android.infantschool.ui.imageloader.GlideRoundTransform;
import com.android.infantschool.ui.view.label.LabelView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class InfromAdapter extends RecyclerView.Adapter<InfromAdapter.MyHolder> {

    private Context mContext;
    public List<InfromBean.DataBean> list = null;
    private OnLister lister;
    private int type = -1;//1:发布的,2:收到的

    public InfromAdapter(Context context, OnLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_infrom, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position) {
        InfromBean.DataBean bean = list.get(position);

        if (bean.getImgUrl() != null) {
            if (bean.getImgUrl().size() > 0) {
                Glide.with(mContext)
                        .load(bean.getImgUrl().get(0))
                        .placeholder(R.mipmap.icon_glide_loading)
                        .error(R.mipmap.icon_glide_filed)
                        .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext, 10))
                        .into(viewHolder.imgInfromPhoto);
            }
        }

        if (bean.getNoticeType() == 1) {
            viewHolder.tvInfromType.setText("家长叮嘱");
        }
        if (bean.getNoticeType() == 2) {
            viewHolder.tvInfromType.setText("活动通知");
        }
        if (bean.getNoticeType() == 3) {
            viewHolder.tvInfromType.setText("家长私信");
        }

        viewHolder.tvInfromTime.setText(bean.getDate());
        viewHolder.tvTitle.setText(bean.getTitle());
        viewHolder.tvInfromReason.setText(bean.getContent());

        if (type == 1) {
            viewHolder.labelView.setVisibility(View.GONE);
            viewHolder.tvInfromType.setVisibility(View.VISIBLE);
        }
        if (type == 2) {
            viewHolder.labelView.setVisibility(View.GONE);
            viewHolder.tvInfromType.setVisibility(View.GONE);
        }

        viewHolder.rlDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lister != null) {
                    lister.lister(position, type);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    public void addDataAll(List<InfromBean.DataBean> list, int type) {
        this.type = type;
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<InfromBean.DataBean> list, int type) {
        this.type = type;
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<InfromBean.DataBean> list) {
        this.list.addAll(0, list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public ImageView imgInfromPhoto;
        public TextView tvInfromType;
        public TextView tvInfromTime;
        public TextView tvInfromReason;
        public TextView tvTitle;
        public RelativeLayout rlDetails;
        public LabelView labelView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgInfromPhoto = itemView.findViewById(R.id.img_infrom_photo);
            tvInfromType = itemView.findViewById(R.id.tv_infrom_type);
            tvInfromTime = itemView.findViewById(R.id.tv_infrom_time);
            tvInfromReason = itemView.findViewById(R.id.tv_infrom_reason);
            tvTitle = itemView.findViewById(R.id.tv_title);
            rlDetails = itemView.findViewById(R.id.rl_details);
            labelView = itemView.findViewById(R.id.label_view);
        }
    }

    public interface OnLister {
        void lister(int position, int type);
    }
}